<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item label="风险点" prop="riskPoint">
        <el-input
          v-model="queryParams.riskPoint"
          placeholder="请输入风险点"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="风险等级" prop="riskLevel">
        <el-select
          v-model="queryParams.riskLevel"
          placeholder="请选择风险等级"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="(value, key) in riskLevelDict"
            :key="key"
            :label="value"
            :value="key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否逾期" prop="isOverdue">
        <el-select
          v-model="queryParams.isOverdue"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="(value, key) in isOverdueDict"
            :key="key"
            :label="value"
            :value="key"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />重置
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      row-key="id"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column label="风险点" align="center" prop="riskPoint" />
      <el-table-column label="风险对象" align="center" prop="riskTarget" />
      <el-table-column label="风险内容" align="center" prop="riskContent" />
      <!-- 新增设备相关字段 -->
      <el-table-column label="设备实例编码" align="center" prop="deviceCode" />
      <el-table-column label="设备所属人" align="center" prop="deviceOwner" />
      <el-table-column label="设备所处单元" align="center" prop="unit" />
      <el-table-column label="单元负责人" align="center" prop="unitLeader" />
      <el-table-column label="风险等级" align="center" prop="riskLevel">
        <template #default="scope">
          <el-button
            :type="{
              低风险: 'primary',
              一般风险: 'info',
              较大风险: 'warning',
              重大风险: 'danger'
            }[scope.row.riskLevel]"
            size="small"
          >
            {{ scope.row.riskLevel }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="排查频次" align="center" prop="checkFrequency">
        <template #default="scope">{{ scope.row.checkFrequency }}天/次</template>
      </el-table-column>
      <el-table-column label="最新排查时间" align="center" prop="lastCheckTime" :formatter="dateFormatter"/>
      <el-table-column label="是否逾期" align="center" prop="isOverdue">
        <template #default="scope">
          <el-tag :type="scope.row.isOverdue ? 'danger' : 'success'">
            {{ scope.row.isOverdue ? '已逾期' : '未逾期' }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { dateFormatter } from '@/utils/formatTime'
import { useI18n } from '@/hooks/web/useI18n'

defineOptions({ name: 'RiskInspect' })
const { t } = useI18n()
const message = useMessage()

// 数据字典
const riskLevelDict = {
  低风险: '低风险',
  一般风险: '一般风险',
  较大风险: '较大风险',
  重大风险: '重大风险'
}

const isOverdueDict = {
  true: '已逾期',
  false: '未逾期'
}

// 列表数据
const loading = ref(true)
const list = ref([
  {
    id: 1,
    riskPoint: '设备维护风险点',
    riskTarget: '数控机床',
    riskContent: '机械臂安全防护',
    deviceCode: 'CNC-2024-001',    // 新增
    deviceOwner: '张伟',           // 新增
    unit: '生产二单元',            // 新增
    unitLeader: '李娜',           // 新增
    riskLevel: '重大风险',
    checkFrequency: 3,
    lastCheckTime: '2024-03-25 10:00:00',
    isOverdue: true
  },
  {
    id: 2,
    riskPoint: '高温熔炉区',
    riskTarget: '熔炉设备',
    riskContent: '温度监控系统',
    deviceCode: 'FURNACE-2024-002', // 新增
    deviceOwner: '王强',            // 新增
    unit: '冶炼单元',               // 新增
    unitLeader: '赵敏',            // 新增
    riskLevel: '较大风险',
    checkFrequency: 7,
    lastCheckTime: '2024-03-28 14:30:00',
    isOverdue: false
  },
  // 新增mock数据开始
  {
    id: 3,
    riskPoint: '机器人安全防护区',
    riskTarget: '工业机器人',
    riskContent: '运动轨迹监控',
    deviceCode: 'ROBOT-2024-003',
    deviceOwner: '陈浩',
    unit: '装配单元',
    unitLeader: '周婷',
    riskLevel: '一般风险',
    checkFrequency: 5,
    lastCheckTime: '2024-03-29 09:15:00',
    isOverdue: false
  },
  {
    id: 4,
    riskPoint: '化学品存储区',
    riskTarget: '储罐',
    riskContent: '泄漏检测系统',
    deviceCode: 'CHEM-2024-004',
    deviceOwner: '杨阳',
    unit: '仓储单元',
    unitLeader: '徐磊',
    riskLevel: '低风险',
    checkFrequency: 10,
    lastCheckTime: '2024-03-30 14:20:00',
    isOverdue: true
  },
  {
    id: 5,
    riskPoint: '电气控制柜',
    riskTarget: '配电系统',
    riskContent: '接地保护装置',
    deviceCode: 'ELEC-2024-005',
    deviceOwner: '黄晓明',
    unit: '动力单元',
    unitLeader: '孙芳',
    riskLevel: '较大风险',
    checkFrequency: 7,
    lastCheckTime: '2024-03-31 16:45:00',
    isOverdue: false
  },
  // 新增mock数据结束
  {
    id: 6,
    riskPoint: '高空作业平台',
    riskTarget: '升降机',
    riskContent: '安全锁止装置',
    deviceCode: 'LIFT-2024-006',
    deviceOwner: '吴刚',
    unit: '维修单元',
    unitLeader: '郑洁',
    riskLevel: '重大风险',
    checkFrequency: 3,
    lastCheckTime: '2024-04-01 08:30:00',
    isOverdue: true
  },
  {
    id: 7,
    riskPoint: '激光切割区',
    riskTarget: '激光设备',
    riskContent: '防护帘完整性',
    deviceCode: 'LASER-2024-007',
    deviceOwner: '林涛',
    unit: '加工单元',
    unitLeader: '王雪',
    riskLevel: '一般风险',
    checkFrequency: 6,
    lastCheckTime: '2024-04-02 10:00:00',
    isOverdue: false
  },
  {
    id: 8,
    riskPoint: '压力容器区',
    riskTarget: '空气压缩机',
    riskContent: '压力表校准',
    deviceCode: 'PRESS-2024-008',
    deviceOwner: '赵勇',
    unit: '动力单元',
    unitLeader: '孙芳',
    riskLevel: '低风险',
    checkFrequency: 8,
    lastCheckTime: '2024-04-03 13:15:00',
    isOverdue: true
  },
  {
    id: 9,
    riskPoint: '传送带作业区',
    riskTarget: '输送机',
    riskContent: '急停按钮响应',
    deviceCode: 'CONVEY-2024-009',
    deviceOwner: '周华',
    unit: '包装单元',
    unitLeader: '李建国',
    riskLevel: '较大风险',
    checkFrequency: 4,
    lastCheckTime: '2024-04-04 15:30:00',
    isOverdue: false
  },
  {
    id: 10,
    riskPoint: '危废存储区',
    riskTarget: '废液桶',
    riskContent: '防渗漏措施',
    deviceCode: 'WASTE-2024-010',
    deviceOwner: '钱伟',
    unit: '环保单元',
    unitLeader: '张敏',
    riskLevel: '重大风险',
    checkFrequency: 2,
    lastCheckTime: '2024-04-05 11:00:00',
    isOverdue: true
  },
  {
    id: 11,
    riskPoint: '锅炉房',
    riskTarget: '蒸汽锅炉',
    riskContent: '水位计监测',
    deviceCode: 'BOILER-2024-011',
    deviceOwner: '孙强',
    unit: '能源单元',
    unitLeader: '王丽',
    riskLevel: '一般风险',
    checkFrequency: 5,
    lastCheckTime: '2024-04-06 09:45:00',
    isOverdue: false
  },
  {
    id: 12,
    riskPoint: '实验室通风',
    riskTarget: '排风系统',
    riskContent: '风速检测',
    deviceCode: 'LAB-2024-012',
    deviceOwner: '李娜',
    unit: '研发单元',
    unitLeader: '陈伟',
    riskLevel: '低风险',
    checkFrequency: 7,
    lastCheckTime: '2024-04-07 14:00:00',
    isOverdue: true
  }
])

const total = ref(12)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  riskPoint: undefined,
  riskLevel: undefined,
  isOverdue: undefined
})

// 获取数据方法
const getList = async () => {
  loading.value = true
  try {
    // 这里调用API接口
    // const { data } = await api.getInspectList(queryParams)
    // list.value = data.list
    // total.value = data.total
  } finally {
    loading.value = false
  }
}

// 搜索/重置
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
/* 可添加自定义样式 */
</style>
